package br.edu.ufcg.dsc.services;

import java.util.Date;

import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import br.edu.ufcg.dsc.domain.Holiday;

public class HolidayService extends BaseService {

	public static int businessDaysBetweenDates(Date start, Date end) {
		Query q = entity.createNativeQuery(
				"SELECT business_Days_Between_Dates(:start, :end)").setParameter(
				"start", start).setParameter("end", end);

		return (Integer) q.getSingleResult();
	}

	public static int countBusinessDaysSince(Date date) {
		Query q = entity.createNativeQuery(
				"SELECT business_Days_Since_Date(:start)").setParameter(
				"start", date);

		return (Integer) q.getSingleResult();
	}

	public static boolean isBusinessDay(Date date) {
		Query q = entity.createNativeQuery("SELECT is_business_day (:day)")
				.setParameter("day", date);

		return (Boolean) q.getSingleResult();
	}

	public static void persist(Date holidayDate) {
		EntityTransaction tx = entity.getTransaction();

		tx.begin();

		try {
			Holiday holiday = new Holiday();

			holiday.setHolidayDate(holidayDate);
			entity.persist(holiday);

			tx.commit();
		} catch (Exception e) {
			tx.rollback();
		}
	}
}
